package com.bootdo.cms.controller;

import cn.hutool.http.HttpResponse;
import com.bootdo.common.config.BootdoConfig;
import com.bootdo.common.domain.FileDO;
import com.bootdo.common.service.FileService;
import com.bootdo.common.utils.*;
import com.bootdo.cms.domain.VideoDO;
import com.bootdo.cms.service.VideoService;
import com.bootdo.system.domain.UserDO;
import org.apache.commons.io.IOUtils;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.*;
import java.net.URLConnection;
import java.time.LocalDateTime;
import java.util.Date;
import java.util.List;
import java.util.Map;

/**
 * 
 * 
 * @author chglee
 * @email 1992lcg@163.com
 * @date 2019-11-12 23:37:44
 */
 
@Controller
@RequestMapping("/cms/video")
public class VideoController {
	@Autowired
	private VideoService videoService;

	@Autowired
	private FileService sysFileService;

	@Autowired
	private BootdoConfig bootdoConfig;
	
	@GetMapping("/index")
	@RequiresPermissions("cms:video:list")
	String Video(){
	    return "cms/videoList/videoList";
	}
	
	@ResponseBody
	@GetMapping("/list")
	@RequiresPermissions("cms:video:list")
	public PageUtils list(@RequestParam Map<String, Object> params){
		//查询列表数据
        Query query = new Query(params);
		List<VideoDO> videoList = videoService.list(query);
		int total = videoService.count(query);
		PageUtils pageUtils = new PageUtils(videoList, total);
		return pageUtils;
	}

	@GetMapping("/add")
	String add(){
	    return "cms/videoList/add";
	}

	@GetMapping("/edit/{id}")
	@RequiresPermissions("cms:video:edit")
	String edit(@PathVariable("id") Long id,Model model){
		VideoDO video = videoService.get(id);
		model.addAttribute("video", video);
	    return "cms/videoList/edit";
	}
	
	/**
	 * 保存
	 */
	@ResponseBody
	@PostMapping("/save")
	@RequiresPermissions("cms:video:upload")
	public R save(VideoDO video){
		UserDO user = ShiroUtils.getUser();
		video.setCreateBy(user.getUserId());
		video.setCreateName(user.getUsername());
		video.setCreateTime(new Date());
		video.setUpdateTime(new Date());
		video.setDelFlag(0);
		if(videoService.save(video)>0){
			return R.ok();
		}
		return R.error();
	}
	/**
	 * 修改
	 */
	@ResponseBody
	@RequestMapping("/update")
	@RequiresPermissions("cms:video:edit")
	public R update( VideoDO video){
		UserDO user = ShiroUtils.getUser();
		video.setUpdateName(user.getUsername());
		video.setUpdateTime(new Date());
		videoService.update(video);
		return R.ok();
	}
	
	/**
	 * 删除
	 */
	@PostMapping( "/remove")
	@ResponseBody
	@RequiresPermissions("cms:video:remove")
	public R remove(Long id){
		VideoDO video = videoService.get(id);
		String url = video.getUrl();
		String path = bootdoConfig.getUploadPath() +
				"video" + File.separator  +
				url + ".mp4";
		File file = new File(path);
		if(file.exists()) {
			file.delete();
		}

		String imgUrl = video.getImgUrl();
		File imgFile = new File(imgUrl);
		if(imgFile.exists()) {
			file.delete();
		}

		if(videoService.remove(id)>0){
		return R.ok();
		}
		return R.error();
	}
	
	/**
	 * 批量删除
	 */
	@PostMapping( "/batchRemove")
	@ResponseBody
	@RequiresPermissions("cms:video:batchRemove")
	public R remove(@RequestParam("ids[]") Long[] ids){
		videoService.batchRemove(ids);
		return R.ok();
	}

	@ResponseBody
	@PostMapping("/upload")
	R upload(@RequestParam("file") MultipartFile file, HttpServletRequest request) {
		String fileName = file.getOriginalFilename();
		fileName = FileUtil.renameToUUID(fileName);
		FileDO sysFile = new FileDO(FileType.fileType(fileName), "/files/image/" + fileName, new Date());
		try {
			FileUtil.uploadFile(file.getBytes(), bootdoConfig.getUploadPath() + "image/", fileName);
		} catch (Exception e) {
			return R.error();
		}

		if (sysFileService.save(sysFile) > 0) {
			return R.ok().put("imgUrl",sysFile.getUrl());
		}
		return R.error();
	}
}
